#!/bin/sh
WAP_INFO_FELE="./wap.info"

UPDATE_HTTP_ADDR=`awk '{if ($1=="HTTP_ADDR") {print $2}}' $WAP_INFO_FELE`
WAPDATA_ETAG=`awk '{if ($1=="ETag:") {print $2}}' $WAP_INFO_FELE`

if [ "$WAPDATA_ETAG" == "" ]; then
	echo "./wap.info is bad file"
	cp recycle/wap.info ./wap.info
fi
gethttphead()
{
	curl -I "$UPDATE_HTTP_ADDR" --speed-time 5 --speed-limit 1
	if [ $? -eq 0 ];then
		Etag=`curl -I "$UPDATE_HTTP_ADDR"`

		echo "$Etag"


		EtagFlag=`echo "$Etag" | grep ETag:.`
		Etag=`echo "$EtagFlag" | awk '{print $2}'`
		
		if [ "$EtagFlag" == "" ]; then
			echo "net Etag can't is NULL"
			return 1;
		fi
		echo "$Etag"
		echo "$WAPDATA_ETAG $UPDATE_HTTP_ADDR"

		if [ "$Etag" == "$WAPDATA_ETAG" ]; then
		   echo "This APP is New"
		else
		#需要更新应用程序
		killall monitoring.sh
		killall wap800_app
		./update.sh
		echo "update result $?"
		if [ $? -eq 0 ];then
			echo "update Etag:$EtagFlag"
			cp ./wap.info recycle/wap.info
			if [ "$EtagFlag" == "" ]; then
			echo "New ETag Can't Is NULL" 
			else
			sed "3c $EtagFlag" ./wap.info -i
			fi
			sleep 1
			cat ./wap.info
			echo "system update finsh ..............................."
			sleep 5
		else
		echo "Update file fail ........................."
		fi
		reboot
		fi
	fi
}

sleep 20
while true   #定个无限循环
do 
	sleep 60
	gethttphead
done

